package com.wzy.tintdrawable;

import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = (TextView) findViewById(R.id.tv);
        ImageView imageView0 = (ImageView) findViewById(R.id.img0);
        ImageView imageView1 = (ImageView) findViewById(R.id.img1);
        ImageView imageView2 = (ImageView) findViewById(R.id.img2);
        Drawable drawable = ContextCompat.getDrawable(this, R.mipmap.copy_icon);

        int[] colors = new int[]{ContextCompat.getColor(this, R.color.colorAccent), ContextCompat.getColor(this, R.color.colorPrimary)};
        int[][] states = new int[2][];
        states[0] = new int[]{android.R.attr.state_pressed};
        states[1] = new int[]{};

        //简单的使用tint改变drawable颜色
        Drawable drawable0 = getTintDrawable(drawable, ContextCompat.getColor(this, R.color.pink));
        imageView0.setImageDrawable(drawable0);

        // 点击变色
        Drawable drawable1 = getStateDrawable(drawable, colors, states);
        imageView1.setImageDrawable(drawable1);

        // 点击变色
        StateListDrawable stateListDrawable = getStateListDrawable(drawable, states);
        Drawable drawable3 = getStateDrawable(stateListDrawable, colors, states);
        imageView2.setImageDrawable(drawable3);
    }

    /**
     * 简单的使用tint改变drawable颜色
     *
     * @param drawable
     * @param color
     * @return
     */
    private Drawable getTintDrawable(Drawable drawable, @ColorInt int color) {
        Drawable.ConstantState state = drawable.getConstantState();
        Drawable drawable1 = DrawableCompat.wrap(state == null ? drawable : state.newDrawable()).mutate();
        drawable1.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        DrawableCompat.setTint(drawable1, color);
        return drawable1;
    }

    /**
     * 点击变色
     *
     * @param drawable
     * @param colors
     * @param states
     * @return
     */
    private Drawable getStateDrawable(Drawable drawable, int[] colors, int[][] states) {
        ColorStateList colorList = new ColorStateList(states, colors);
        Drawable.ConstantState state = drawable.getConstantState();
        drawable = DrawableCompat.wrap(state == null ? drawable : state.newDrawable()).mutate();
        DrawableCompat.setTintList(drawable, colorList);
        return drawable;
    }

    /**
     * 点击变色
     *
     * @param drawable
     * @param states
     * @return
     */
    @NonNull
    private StateListDrawable getStateListDrawable(Drawable drawable, int[][] states) {
        StateListDrawable stateListDrawable = new StateListDrawable();
        for (int[] state : states) {
            stateListDrawable.addState(state, drawable);
        }
        return stateListDrawable;
    }
}
